//
//  SetViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/2/25.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SetViewController.h"

#import "Scan_VC.h"
#import "UIView+SDExtension.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "WeViewController.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UIApplicationDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *dataArr;
@property(nonatomic, retain)UIView *backView;
@end

@implementation SetViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_backView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationItem.title = @"设置";
    self.dataArr = @[@"推送开关",@"关于我们",@"版本更新",@"扫一扫",@"短信验证",@"打电话",@"发短信"];
    [self creatView];
    
    
   
    //重写左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];

   
    
    
}

-(void)leftAction:(UINavigationItem *)le
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor =[ UIColor whiteColor];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark -分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}
#pragma mark -分区头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
#pragma mark -行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark -cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = _dataArr[indexPath.section];
    /**推送开关控制器*/
    if (indexPath.section == 0) {
        
        UISwitch *switchcontroll = [[ UISwitch  alloc] init];
        [switchcontroll addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = switchcontroll;
        [switchcontroll release];
    }
  
    //取消点击时的样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



#pragma mark -行高
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark -cell点击状态
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**版本更新*/

    if (indexPath.section == 2) {
        [self showTextDialog];
        
    }
    /**扫一扫*/
    if(indexPath.section == 3)
    {
        Scan_VC *scanCode = [[Scan_VC alloc] init];
        [self presentViewController:scanCode animated:YES completion:^{
            
        }];
    }
    /**短信验证*/
    if(indexPath.section == 4)
    {
        LoginViewController *login =[[  LoginViewController alloc] init];
        [self.navigationController pushViewController:login animated:YES];
    }
    /**关于我们*/
    if( indexPath.section == 1)
    {
        WeViewController *we =[[ WeViewController alloc] init];
        [self.navigationController pushViewController:we animated:YES];
    }
    /**打电话*/
    if (indexPath.section == 5) {
        NSURL *url = [NSURL URLWithString:@"tel://18246174579"];
        
        [[UIApplication sharedApplication] openURL:url];
    }
    /**发短信*/
    if(indexPath.section == 6)
    {
        NSURL *url = [NSURL URLWithString:@"sms://18246174579"];
        [[UIApplication sharedApplication] openURL:url];
    }
   }
//推送开关
-(void)switchAction:(UISwitch *)s
{//开启时
    if (s.on) {
        [SetViewController registerLocalNotification:4];// 4秒后
    }
}
//加载进度
-(void)showTextDialog
{
    //HUD（Head-Up Display，意思是抬头显示的意思）
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = YES;
    
    //设置对话框文字
    HUD.labelText = @"请稍等";
    
    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);
        [self alertWithMessage:@"已经是最新版本了"];

    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];
        [HUD release];
     //   HUD = nil;
    }];

   //
}
#pragma mark -提示框
- (void)alertWithMessage:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    // 给alert添加操作
    [alert addAction:okAction];
    // 模态显示
    [self presentViewController:alert animated:YES completion:nil];
}

// 设置本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime {
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知的时间
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
    NSLog(@"fireDate=%@",fireDate);
    
    notification.fireDate = fireDate;
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    // 设置重复的间隔
    notification.repeatInterval = kCFCalendarUnitSecond;
    
    // 通知内容
    notification.alertBody =  @"好吃的来了...";
    notification.applicationIconBadgeNumber = 1;
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 通知参数
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"欢迎来到美食天地" forKey:@"key"];
    notification.userInfo = userDict;
    
    // ios8后，需要添加这个注册，才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重复提示的单位，可以是天、周、月
        notification.repeatInterval = NSCalendarUnitDay;
    } else {
        // 通知重复提示的单位，可以是天、周、月
        notification.repeatInterval = NSDayCalendarUnit;
    }
    
    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

// 取消某个本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[key];
            
            // 如果找到需要取消的通知，则取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
